package cn.xfdzx.android.apps.shop.adapter;

import android.bluetooth.BluetoothDevice;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;

/**
 * Creator: zf.
 * Date: 2020/9/3.
 * Description:蓝牙设备列表.
 */
public class BlueDevicListAdapter extends BaseQuickAdapter<BluetoothDevice, BaseViewHolder> {

    private OnItemOnClickListener onItemOnClickListener;
    private List<BluetoothDevice> dataBeans = new ArrayList<>();//
    private int selPosition = -1;
    private String collectionName = "";//连接打印机名称

    public BlueDevicListAdapter(@Nullable List<BluetoothDevice> data) {
        super(R.layout.item_device_name, data);
        this.dataBeans = data;
    }

    @Override
    public void setData(int index, @NonNull BluetoothDevice data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final BluetoothDevice date) {
        TextView tv_collection = viewHolder.getView(R.id.tv_collection);


        if (EmptyUtil.isNotEmpty(date.getName())){
            viewHolder.setText(R.id.tv_name,date.getName());
            if (collectionName.equals(date.getName())){
                tv_collection.setText("已连接");
            }else {
                tv_collection.setText("连接");
            }
        }else {
            viewHolder.setText(R.id.tv_name,date.getAddress());
        }

//        if (selPosition>=0){
//            if (selPosition == viewHolder.getLayoutPosition()){
//                tv_collection.setText("已连接");
//            }else {
//                tv_collection.setText("连接");
//            }
//        }

        tv_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemOnClickListener.onItemConnectionClick(date,viewHolder.getLayoutPosition());
            }
        });


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<BluetoothDevice> list){
        this.dataBeans = list;
        super.replaceData(list);
    }

    public void setOnItemConnectionListener(OnItemOnClickListener onItemCollection) {
        this.onItemOnClickListener = onItemCollection;
    }

    public void setSel(int currentPosition) {
        this.selPosition = currentPosition;
        notifyDataSetChanged();
    }

    public void setCollection(String currentName) {
        this.collectionName = currentName;
        notifyDataSetChanged();
    }

    public interface OnItemOnClickListener {
        void onItemConnectionClick(BluetoothDevice itemDate,int position);
    }
}

